<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class district extends CI_Controller{
      protected $_templates;
      function __construct() {
          parent::__construct();
          $this->pre_message = "";
          $this->session->set_userdata(array('Url'=>uri_string()));
		  $this->lang->load('district');
          $this->load->model('district_model','district');
		  $this->load->model('city_model','city');
		  $this->load->helper('upload_file');
      }   
	  function index(){
		  $this->acl->check('view','mod_district','district',base_url());
		  $this->listdistrict();
	  }
      function listdistrict(){
         	 $this->acl->check('view','mod_district','district',base_url());
            /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('name'=>'Tiêu đề'),'district/listdistrict/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
               
            }else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
            /*End  search*/
            $data['title'] = 'Danh sách tỉnh thành';
			if($this->acl->check('add','mod_district','district'))
            	$data['add'] = 'district/add';
            $config['base_url'] = base_url().'district/listdistrict/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->district->get_num_data($arr_search);
            $config['total_rows']   =  $data['num'];
            $config['per_page']  =   '20';
            $config['uri_segment'] = 4;   
            $this->pagination->initialize($config);   
            $data['list'] =   $this->district->get_all_data($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
            $data['pagination']    = $this->pagination->create_links();            
            $this->_templates['page'] = 'district/listdistrict';	
           if($type=="ajax")
		   {
                echo $this->load->view($this->_templates['page'],$data,true);
		   }else
                $this->site_lib->load($this->_templates['page'],$data); 
      }
      
      function add(){    
	   		$this->acl->check('add','mod_district','district',base_url());                     
            $data['title'] = 'Thêm mới bài viết';
			$data['option_pid'] = $this->city->optionCity();
            $this->form_validation->set_rules('name',lang('district_name'),'trim|required');
			
		
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 $uploadDir = "../uploads/city/district/";
				 $files = $_FILES['uploadfile'];
				 $img_name = settings_upload_avatar($files,$uploadDir);
				$data = $this->build_data($_POST,1);
				 if($this->district->saveData(0,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('district/listdistrict');
				 }else{
					 
					 $this->pre_message = 'Lưu không thành công';
				 }
					 
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'district/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){     
	   		$this->acl->check('edit','mod_district','district',base_url());                    
            $data['title'] = 'Sửa tỉnh thành';
			$data['query'] = $this->district->get_object($id);
			$data['option_pid'] = $this->city->optionCity($data['query']->city_id);
            $this->form_validation->set_rules('name',lang('district_name'),'trim|required');
			
			
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				$data = $this->build_data($_POST,0);
				 if($this->district->saveData($id,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('district/listdistrict');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				 }
			  
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'district/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
		$this->acl->check('del','mod_district','district',base_url());
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->district->deleted($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('district/listdistrict/'.$page);
    }
    function dels(){
		 $this->acl->check('dels','mod_district','district',base_url());
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->district->deleted($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('district/listdistrict/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
		$dataUpdate["city_id"]=trim($data["city_id"]);
		$dataUpdate["name"]=trim($data["name"]);
		$dataUpdate["name_en"]=trim($data["name_en"]);
		$dataUpdate["description"]=trim($data["description"]);
		$dataUpdate["description_en"]=trim($data["description_en"]);
		$dataUpdate["nb_order"]=trim($data["nb_order"]);
		if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}
    
  }
?>
